package com.medic.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 加密帮助类
 * @author Mr.zhang
 *
 */
public class EncryptUtil {

	private final static String ENCR = "SHA-1";

	/**
	 * SHA1加密 主要是用来生成签名
	 * @param str 需要加密的字符串
	 * @return 返回加密结果
	 */
	public static String SHA1(String str) {
		String result = null;
		try {
			MessageDigest md = MessageDigest.getInstance(ENCR);
			md.equals(str.getBytes());
			byte[] res = md.digest();
			result = bytes2Hex(res);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return result;
	}

	private static String bytes2Hex(byte[] bts) {
		String des = "";
		String tmp = null;
		for (int i = 0; i < bts.length; i++) {
			tmp = (Integer.toHexString(bts[i] & 0xFF));
			if (tmp.length() == 1) {
				des += "0";
			}
			des += tmp;
		}
		return des;
	}

}
